﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QUANLYNHASACH
{
    public partial class frmQUANLYTHELOAI : Form
    {
        QUANLYNHASACH.App_code.BLL.THELOAI_BLL bllTheLoai = new QUANLYNHASACH.App_code.BLL.THELOAI_BLL();
        public frmQUANLYTHELOAI()
        {
            InitializeComponent();
        }

        private void frmQUANLYTHELOAI_Load(object sender, EventArgs e)
        {
            dtgTheLoai.DataSource = bllTheLoai.LayDanhSachTheLoai();
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            try
            {
               
                    string idTheLoai = "000" + txtID.Text;
                    string tentheloai = txtTheLoai.Text;
                    bllTheLoai.ThemTheLoai(idTheLoai, tentheloai);
                    MessageBox.Show("Thành công!", "thông báo");
                    dtgTheLoai.DataSource = bllTheLoai.LayDanhSachTheLoai();

               
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
            }
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            try
            {
                bool isCheck = false;
                for (int i = 0; i < dtgTheLoai.Rows.Count; i++)
                {

                    if (dtgTheLoai.Rows[i].Cells[0].Value != null)
                    {
                        isCheck = (bool)dtgTheLoai.Rows[i].Cells[0].Value;
                        string idTheLoai = dtgTheLoai.Rows[i].Cells[1].Value.ToString();
                        if (isCheck == true)
                        {
                           bllTheLoai.xoaTheLoai(idTheLoai);

                        }
                    }
                }
                MessageBox.Show("Oh yeah!", "thông báo");
                dtgTheLoai.DataSource = bllTheLoai.LayDanhSachTheLoai();


            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
            }
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            try
            {
                bool isCheck = false;
                for (int i = 0; i < dtgTheLoai.Rows.Count; i++)
                {
                    
                    if (dtgTheLoai.Rows[i].Cells[0].Value != null)
                    {
                        isCheck = (bool)dtgTheLoai.Rows[i].Cells[0].Value;
                        string idTheLoai = dtgTheLoai.Rows[i].Cells[1].Value.ToString();
                        string tentheloai = dtgTheLoai.Rows[i].Cells[2].Value.ToString();
                        if (isCheck == true)
                        {
                           bllTheLoai.suaTheLoai(idTheLoai, tentheloai);
                           
                        }
                    }
                }
                MessageBox.Show("Thành công!", "thông báo");
                dtgTheLoai.DataSource = bllTheLoai.LayDanhSachTheLoai();


            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
            }
        }
    }
}
